import os
import time
import psutil
import socket
import threading
import atexit
from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_COMPLETED, ALL_COMPLETED

executor = ThreadPoolExecutor()
try:
    import Queue as queue
except ImportError:
    import queue

__SCALE_DICT__ = {
    "GB": 1073741824,
    "MB": 1048576,
    "KB": 1024,
    "bytes": 1
}


def _trans(number, places=6):
    for unit, scale in __SCALE_DICT__.items():
        if number > scale:
            return round(number / scale, places), unit
    return number, "bytes"


def main():
    print(get_total_meminfo())
    all_task = [executor.submit(get_cpuinfo, run_id) for run_id in range(1)]
    print("ok1")
    print(get_netinfo())
    wait(all_task, return_when=FIRST_COMPLETED)
    ans = executor.submit(get_cpuinfo)
    # ans = ""
    # for future in as_completed(all_task):
    #     ans.join(str(future.result()))
    print(ans)


def get_hostname():
    return socket.gethostname()


def get_total_meminfo():
    with open("/proc/meminfo") as f:
        tmp = int(f.readline().split()[1])
        return tmp / 1024


def get_meminfo():
    tmp_ans = psutil.virtual_memory()
    total = _trans(tmp_ans.total), tmp_ans.percent, _trans(tmp_ans.used), _trans(tmp_ans.available)
    return total


def get_cpuinfo(run_id: int = -1):
    cnt = (psutil.cpu_count(), psutil.cpu_count(logical=False))
    percent = psutil.cpu_percent(3)
    return cnt, percent


def get_netinfo():
    total = psutil.net_io_counters()
    return total

# main()
